﻿using System;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using Pharmacy.Data;
using Pharmacy.Entities;
using Pharmacy.Windows.Forms.Main;

namespace Pharmacy.Windows.Forms
{
    public partial class FormHistory : DevExpress.XtraEditors.XtraForm
    {
        public Prescription PreviewDN;

        public FormHistory()
        {
            InitializeComponent();
            TList<DrugDispo> all = DataRepository.DrugDispoProvider.GetAll();
            cgDrugId.Properties.DisplayMember = "DrugName";
            cgDrugId.Properties.ValueMember = "DrugId";
            cgDrugId.Properties.DataSource = all;
            cgDrugId.EditValue = DBNull.Value;
            //btn_Refresh_Click(null, null);

        }

        private void btn_Search_Click(object sender, System.EventArgs e)
        {
            string WhereExpression = "";
            if (tb_FromDate.Text != "" && tb_ToDate.Text != "")
                WhereExpression = "DeliveryDate Between N'" + tb_FromDate.DateTime.ToString("yyyy-MM-dd 00:00:00") + "' And N'" + tb_ToDate.DateTime.ToString("yyyy-MM-dd 23:59:59") + "'";
            if (tb_PatientCode.Text.Length == 6)
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PatientCode=N'" + tb_PatientCode.Text + "'";
                else
                    WhereExpression = "PatientCode=N'" + tb_PatientCode.Text + "'";
            if (tb_PatientName.Text.Length > 0)
            {
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PatientName LIKE N'%" + tb_PatientName.Text + "%'";
                else
                    WhereExpression = "PatientName LIKE N'%" + tb_PatientName.Text + "%'";
            }
            if (tb_DNId.Text.Length > 0)
            {
                if (WhereExpression.Length > 0)
                    WhereExpression += "And PrescriptionId LIKE N'" + tb_DNId.Text + "%'";
                else
                    WhereExpression = "PrescriptionId LIKE N'" + tb_DNId.Text + "%'";
            }
            if (!String.IsNullOrEmpty(cgDrugId.EditValue.ToString()))
            {
                if (WhereExpression.Length > 0)
                    WhereExpression += "And DrugId ='" + cgDrugId.EditValue + "'";
                else
                    WhereExpression = "DrugId ='" + cgDrugId.EditValue + "'";
            }

            int count = 0;
            gridControl1.DataSource = DataRepository.DeliveryNoteHistoryProvider.GetPaged(WhereExpression,
                                                                                          "DeliveryDate", 0, 2000,
                                                                                          out count);
        }

        private void btnPreview_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dnHistory  = (DeliveryNoteHistory)gridView1.GetRow(gridView1.FocusedRowHandle);
            var prescription = DataRepository.PrescriptionProvider.GetByPrescriptionId(dnHistory.PrescriptionId);
            DataRepository.PrescriptionProvider.DeepLoad(prescription, true, DeepLoadType.IncludeChildren, typeof(TList<PrescriptionDetail>));
            FormPrescriptionPreview objPreview = new FormPrescriptionPreview();
            objPreview.LoadReport(prescription, false, "");
            objPreview.Show();
            //Utilities.PreviewDeliveryNote(prescription, reportViewer2);
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            tb_FromDate.Text = tb_ToDate.Text = tb_DNId.Text = tb_PatientName.Text = tb_PatientCode.Text = "";
            cgDrugId.EditValue = DBNull.Value;
        }

        private void btn_ExportToExcel_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog()==DialogResult.OK)
            {
                gridControl1.MainView.ExportToXls(saveFileDialog1.FileName);
            }

        }

        private void btnLabel_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dnHistory = (DeliveryNoteHistory)gridView1.GetRow(gridView1.FocusedRowHandle);
            var formPrescriptionLabel = new FormPrescriptionLabel();
            formPrescriptionLabel.LoadDefault(dnHistory.PrescriptionId);
            formPrescriptionLabel.Show();
        }
    }
}